package com.screenrouter.controller;

import com.screenrouter.model.ScreenConfig;
import com.screenrouter.service.ScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/screen")
@CrossOrigin(origins = "*")
public class ScreenController {
    
    @Autowired
    private ScreenService screenService;
    
    @GetMapping("/config")
    public ScreenConfig getConfig() {
        return screenService.getCurrentConfig();
    }
    
    @PostMapping("/config")
    public ScreenConfig updateConfig(@RequestBody ScreenConfig config) {
        screenService.updateConfig(config);
        return screenService.getCurrentConfig();
    }
    
    @PostMapping("/switch/{pageName}")
    public void switchPage(@PathVariable String pageName) {
        screenService.switchToPage(pageName);
    }
    
    @PostMapping("/mode/{mode}")
    public void setMode(@PathVariable String mode) {
        screenService.setMode(mode);
    }
    
    @PostMapping("/interval/{interval}")
    public void setInterval(@PathVariable Integer interval) {
        screenService.setInterval(interval);
    }
    
    @MessageMapping("/switch-page")
    @SendTo("/topic/screen-switch")
    public String handlePageSwitch(String pageName) {
        screenService.switchToPage(pageName);
        return pageName;
    }
    
    @MessageMapping("/update-config")
    @SendTo("/topic/screen-config")
    public ScreenConfig handleConfigUpdate(ScreenConfig config) {
        screenService.updateConfig(config);
        return screenService.getCurrentConfig();
    }
} 